<?php
namespace Home\Controller;
use Think\Controller;
// +----------------------------------------------------------------------
// | Date:2016年12月21日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class SearchController extends BaseController {
    
    public function index($pat='') {
        $productModel = M("Product");
        $cateModel = M("Category");
        $fromyear = I('Fromyear');
        $toyear = I('Toyear');
        $order = 'id desc';
        
        switch ($pat){
            case 'newest' :
                $order = 'published DESC';//最新作品
                break;
            case 'hot' :
                $order = 'hits DESC';//点击数排序
                break;
            case 'random' :
                $order = 'RAND()';//随机
            case 'custom' :
                $cateId = D('Admin/Config')->getConfig('pro_index');//自定义类别
                $where['t_hy|t_lx|t_xq|t_gj|t_jx'] = $cateId['custom_cate_id'];
                break;
            case 'xfzdc' :     //消费者洞察
                $where['content'] = ['neq',' '];
                $order = 'published DESC';
                break;
            case 'recommend' :       //精选案例
                $where['recommend_type'] = 1;
                $order = 'update_time DESC';
                break;
            case 'classics' :       //经典案例
                $where['recommend_type'] = ['in',[0,2]];
                $order = 'recommend_type DESC, id DESC';//点击数排序
                break;
                
            default:break;    
        }
        
        /*分类栏查询的 分类按钮显示/触发动作/删除查询记录*/
        $cateCondition = $this->_search_category();
        if ($cateCondition) $where = $cateCondition; //返回的查询条件写入where数组
        $where['status'] = 1;
        
        /*处理时间查询*/
        if (!empty($fromyear) && !empty($toyear)) {
            for($x=$fromyear; $x<=$toyear; $x++){
                $years[]=$x."年";
            }
            $year_cateid=$cateModel->where(['name'=>['in',$years]])->getField('cid',true);
            $where['t_sj'] = ['in',$year_cateid];
        }
        
        /* 搜索输入 框关键词查询 处理*/
        $where = $this->_search_keyword_bar($where);
        $count = $productModel->where($where)->count();
        $page = new \Think\Page($count,20);
        
        $showPage = $page->show();
        $this->assign("page", $showPage);
        $list=$productModel->where($where)->order($order)->limit($page->firstRow, $page->listRows)->select();
        /* 显示分类筛选栏*/
        R('Public/category');
        $list = D('Product')->getCover($list);
        $this->assign("list", $list);
        $this->assign('webtitle',L('T_HOME'));
        $this->display();
    }
    
    
    /**
     * 关键词搜索框功能执行
     * 返回查询条件数组
     * date:2017年1月16日
     * author: EK_熊
     */
    private function _search_keyword_bar($where){
        foreach ($this->searchBar as $k=>$v){
            $search_val = I($k);
            if (!empty($search_val)) {
                if ($k == 'country') {
                    $where['t_gj'] = M("Category")->where(['pid'=>3,'name'=>$search_val])->getField('cid');
                }elseif($k == 'all'){
                    $where['title|agency|brand|keywords|summary'] = ['like',"%{$search_val}%"];
                }else{
                    $where[$k] = ['like',"%{$search_val}%"];
                }
                
                //添加关键词查询日志记录
                D('SearchLog')->search_add($search_val,$this->uid,$k);
                
                $this->assign('searchBarVal',$search_val);
                $this->assign('searchBarKey',$k);
                $this->assign('searchBarName',$v);
                break;
            }
        }
        return $where;
    }
    
    /**
     * 设置/删除，分类筛选查询条件
     * 
     * date:2016年12月23日
     * author: EK_熊
     */
    private function _search_category(){
        $field = ['t_xq','t_hy','t_gj','t_sj','t_jx','t_lx'];
        $searchLog = I('search',[]);
        $searchDel = I('searchDel');//是否执行删除单个选项
        if (!empty($searchLog)) {
            parse_str(urldecode(I('search')),$searchLog);//函数把查询字符串解析到变量中
        }
        foreach ($field as $k=>$v){
            $queryValue = I($v);
            if (!$queryValue) continue;
            if ($searchDel){
                  unset($searchLog[$v]);              
            }else{
                $searchLog[$v] = $queryValue;
            }
            
        }
        
        if ($searchLog) {
            $searchLogName = M('Category')->where(['cid'=>['in',$searchLog]])->getField('cid,name',true);
            if ($searchLogName) $this->assign('searchIdLogName',$searchLogName);
            $this->assign('search',urlencode(http_build_query($searchLog)));
            $this->assign('searchIdLog',$searchLog);
            if ($searchLog['t_jx']) $searchLog['t_jx'] = ['like',"%{$searchLog['t_jx']}%"];//奖项字段存放多个值
            if ($searchLog['t_hy']){
                $cat = new \Org\Util\Category(M('Category')->order('listorder asc'), array('cid', 'pid', 'name','name'));
                $cat_arr = $cat->getList('',$searchLog['t_hy']);//获取分类结构
                $t_hy_id = [$searchLog['t_hy']];
                foreach ($cat_arr as $k=>$v){
                    $t_hy_id[] = $v['cid'];
                }
                $searchLog['t_hy'] = ['in',$t_hy_id];//行业得获取整个树状结构id
            }
            
        }
        
        return $searchLog;
    }
    
    
    

}